package demo;

import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import org.weixin4j.Configuration;

/**
 * @author 赵飞
 * @version V1.0
 * @Title:
 * @Package demo
 * @Description:
 * @date 2017/2/15
 */
public class DecryptUtil {

	public static void main(String args[]) throws Exception{


		String msgSignature = "262f90480be27dc163e40b6c9fd1ec47dea28a5e";
		String timestamp = "1487152598";
		String nonce = "778792762";
		String fromXML = "<xml><AppId><![CDATA[wx964f044ff3a4e329]]></AppId><Encrypt><![CDATA[ZuhnHddEs39jp+tgRyalmEoTpkfEgrS9sKI3VzcZaplOQ5QF7y+2YaJP1BY0cQFvoM0JpOV2XT8rzLzfweW0jIb61ouMBxqmTKogu413dzWAXqGBgVgB+7LN0TFkeOAgpe5lSu070KeIVdw0V6mWRdYBPUJBGVWgrUTYGiRJmoK+7MGhMBg38/l1UKO8wd9k7pcgL+1/K87WeMLJKP4joQLaXcdBCXjaXpykDZ1OAInPSEdEeFP96vqFnvHoM9c0w5BdS8CTqIyZud8nEyX2tjAZtnUQmLPoKrNOzO9/11yjiM0FKd33Yny/LA82XtTVOG0EVyPxTfAFI4Vhp3yzvJALt8uBB8RbUpEjDaAvzrrFeajhGaWTS7MJiHRqV+DUoXUagPQFds7CHEQb7vrjUCWPdWT5nJjW45H6iH435hG5F/0Q/nPL//MatCExsPWWLBo7VZsfkCsnOqYGeirCZw==]]></Encrypt></xml>";

		System.out.println("解密后明文: \n" + decryptMsg(msgSignature,timestamp,nonce,fromXML));
	}



	/**
	 *密文：
	 imestamp=[1487152598];nonce=[778792762];encrypt_type=[aes];msg_signature=[262f90480be27dc163e40b6c9fd1ec47dea28a5e]接收到微信输入流,准备处理...
	 <xml><AppId><![CDATA[wx964f044ff3a4e329]]></AppId><Encrypt><![CDATA[ZuhnHddEs39jp+tgRyalmEoTpkfEgrS9sKI3VzcZaplOQ5QF7y+2YaJP1BY0cQFvoM0JpOV2XT8rzLzfweW0jIb61ouMBxqmTKogu413dzWAXqGBgVgB+7LN0TFkeOAgpe5lSu070KeIVdw0V6mWRdYBPUJBGVWgrUTYGiRJmoK+7MGhMBg38/l1UKO8wd9k7pcgL+1/K87WeMLJKP4joQLaXcdBCXjaXpykDZ1OAInPSEdEeFP96vqFnvHoM9c0w5BdS8CTqIyZud8nEyX2tjAZtnUQmLPoKrNOzO9/11yjiM0FKd33Yny/LA82XtTVOG0EVyPxTfAFI4Vhp3yzvJALt8uBB8RbUpEjDaAvzrrFeajhGaWTS7MJiHRqV+DUoXUagPQFds7CHEQb7vrjUCWPdWT5nJjW45H6iH435hG5F/0Q/nPL//MatCExsPWWLBo7VZsfkCsnOqYGeirCZw==]]></Encrypt></xml>
	 */

	/**
	 * 解密后:
	 * <xml><AppId><![CDATA[wx964f044ff3a4e329]]></AppId>
	 <CreateTime>1487152598</CreateTime>
	 <InfoType><![CDATA[component_verify_ticket]]></InfoType>
	 <ComponentVerifyTicket><![CDATA[ticket@@@GfGtRkgbkrDWlQe7RmERX7IE8_29nZBRRNmTqDxq9Ja0FI0ZpxD-j9XJPkxHEj1nSMC-lnyos6gR9jPO5uNG5g]]></ComponentVerifyTicket>
	 </xml>
	 */

	/**
	 * 进行消息推送密文解密测试
	 */
	public static String decryptMsg(String msgSignature,String timestamp,String nonce,String fromXML) throws Exception{
		String token = Configuration.getProperty("weixin4j.token");
		String encodingAesKey = Configuration.getProperty("encodingAesKey");
		String appId = Configuration.getProperty("weixin4j.oauth.appid");

		WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
		String result2 = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
		return result2;
	}
}
